package com.chwn.curiousdiary.ui.play.fragment;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.chwn.curiousdiary.R;
import com.chwn.curiousdiary.bean.StoryBean;
import com.chwn.curiousdiary.gloable.CustomEvent;
import com.chwn.curiousdiary.utils.ImgLoadUtil;
import com.chwn.harulib.adapter.ViewHolder;
import com.chwn.harulib.adapter.base.CommonBaseAdapter;
import com.chwn.harulib.adapter.interfaces.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Myshikin on 2017/4/20.
 */

public class PlayQueueFragment extends DialogFragment {

    public  Activity              mContext;
    private RecyclerView          mPlayListRV;
    private LinearLayoutManager   layoutManager;
    private ArrayList<StoryBean>  list;
    private int                   mCurrentPlayPosition;
    private int                   bgType;
    private PlayListAdapter       playListAdapter;

    private ImageView mBgIV;

    private int[] bgIds = new int[]{R.drawable.bg_queue_adventure, R.drawable.bg_queue_build, R.drawable.bg_queue_fancy
            , R.drawable.bg_queue_life, R.drawable.bg_queue_china, R.drawable.bg_queue_animal, R.drawable.bg_queue_adventure,};

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.mContext = activity;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_FRAME, R.style.CustomDatePickerDialog);


    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //设置无标题
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置从底部弹出
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        window.requestFeature(Window.FEATURE_NO_TITLE);
        window.setAttributes(params);
        window.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.play_queue_bg)));

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

        View view = inflater.inflate(R.layout.fragment_queue, container);

        mPlayListRV = (RecyclerView) view.findViewById(R.id.play_list);
        mBgIV = (ImageView) view.findViewById(R.id.iv_bg);
        view.findViewById(R.id.iv_close_queue).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        layoutManager = new LinearLayoutManager(mContext);
        mPlayListRV.setLayoutManager(layoutManager);
//        mPlayListRV.setHasFixedSize(true);

        return view;
    }

    private void getData() {
        Bundle arguments = getArguments();
        list = (ArrayList<StoryBean>) arguments.getSerializable("list");
        mCurrentPlayPosition = arguments.getInt("position");
        bgType = arguments.getInt("bgType", 0);

        mBgIV.setBackgroundResource(bgIds[bgType]);
    }

    @Override
    public void onStart() {
        super.onStart();
        //设置fragment高度 、宽度
        int dialogHeight = (int) (mContext.getResources().getDisplayMetrics().heightPixels * 0.5);
        getDialog().getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, dialogHeight);
        getDialog().setCanceledOnTouchOutside(true);
        getData();
        playListAdapter = new PlayListAdapter(getActivity(), list, false);
        playListAdapter.setOnItemClickListener(new OnItemClickListener<StoryBean>() {
            @Override
            public void onItemClick(ViewHolder viewHolder, StoryBean data, int position) {
                EventBus.getDefault().post(new CustomEvent(CustomEvent.START_STORY_PLAY, position));
                //                dismiss();
            }
        });
        mPlayListRV.setAdapter(playListAdapter);

    }

    @Override
    public void onResume() {
        super.onResume();
        mCurrentPlayPosition = getArguments().getInt("position");
        playListAdapter.notifyDataSetChanged();
    }

    /**
     * @param event
     */
    public void onEventMainThread(CustomEvent event) {
        switch (event.getState()) {
            case CustomEvent.STORY_PLAY_POSITION_CHANGE:
                if (playListAdapter != null) {
                    int position = event.getPosition();
                    playListAdapter.notifyItemChanged(mCurrentPlayPosition);
                    playListAdapter.notifyItemChanged(position);
                    mCurrentPlayPosition = position;
                }
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }

    }


    public class PlayListAdapter extends CommonBaseAdapter<StoryBean> {


        public PlayListAdapter(Context context, List<StoryBean> datas, boolean isOpenLoadMore) {
            super(context, datas, isOpenLoadMore);
        }

        @Override
        protected void convert(ViewHolder holder, StoryBean data) {

        }

        @Override
        protected void convert(ViewHolder holder, final StoryBean data, int position) {
            ImageView mIVStory = holder.getView(R.id.iv_story_img);
            ImgLoadUtil.displayCircle(mIVStory, data.getStoryImage());
            TextView view = holder.getView(R.id.tv_story_name);
            view.setText(data.getStoryTitle());

            if (mCurrentPlayPosition == position) {
                view.setTextColor(getResources().getColor(R.color.play_list_text_playing));
                holder.setViewVisibility(R.id.play_state);
            } else {
                view.setTextColor(getResources().getColor(R.color.app_bg_white));
                holder.setViewGone(R.id.play_state);
            }
        }

        @Override
        protected int getItemLayoutId() {
            return R.layout.item_play_list;
        }
    }

}
